//便捷处理输入参数与回调
const default_out = {
    data() {
        return {
            router_params_load: false,
            intoPath: null,
            cache_into_params: {},
            isConfirm_routeParamsChange: true,
        };
    },
    created() {
        // this.refresh_into_params()

    },
    mounted() {
        // this.route_activated()
    },
    computed: {
        current_into_params() {
            return Object.assign({}, this.$route.query, this.$route.params)
        },
    },
    activated() {
        this.route_activated()
    },

    methods: {
        route_activated() {
            let { emitKey: newEmit, ...newParams } = this.current_into_params
            let { emitKey: oldEmit, ...oldParams } = this.cache_into_params

            if (!this.router_params_load) {
                this.router_params_load = true
                this.refresh_into_params()
                this.init_data_default(newParams)
                return
            }
            // if (_.isEmpty(oldParams)||_.isEmpty(newParams)) {
            //     this.router_params_load = true
            //     this.refresh_into_params()
            //     this.init_data_default(newParams)
            //     return
            // }
            if (this.check_into_params(newParams, oldParams)) {
                if (this.isConfirm_routeParamsChange) {
                    this.$confirm(this.$tt('tip_check_params_change_need_refresh_page'), this.$tt('label_tip'), {
                        type: 'warning'
                    }).then(() => {
                        this.refresh_into_params()
                        this.init_data_default(newParams)
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: this.$tt('tip_has_cancel_refresh')
                        });
                    });
                } else {
                    this.refresh_into_params()
                    this.init_data_default(newParams)
                }
            }
        },

        check_into_params(newParams, oldParams) {
            return JSON.stringify(newParams) != JSON.stringify(oldParams)
        },
        refresh_data() {
            let { emitKey: newEmit, ...newParams } = this.current_into_params
            this.init_data_default(newParams)
        },
        init_data_default() {

        },
        refresh_into_params() {
            //缓存进入路径
            this.intoPath = this.$store.state.prevRoute.path
            //缓存进入参数
            this.cache_into_params = this.current_into_params
            this.$tag.tagModify({
                query: this.$route.query,
                params: this.$route.params,

            }, null)

        },
        //自动化调用回调，key可为emit-key也可为callback函数。优先调用函数
        return_emit(key, params) {
            if (key) {
                if (this.cache_into_params[key] instanceof Function) {
                    this.cache_into_params[key](params)
                } else {

                    this.$emitPage(key, params)
                }
            } else {
                let back = this.cache_into_params.callback || this.cache_into_params.emitKey
                if (!back) {
                    console.error("emit key is null")
                    return
                }
                if (back instanceof Function) {
                    back(params)
                } else {

                    this.$emitPage(back, params)

                }
            }
        },
        return_page_back() {
            this.$tag.tagClose(null, this.intoPath,true)
        },
    }
}
export default default_out
